angular.
  module('searchAccount').
  component('searchAccount', {
    templateUrl:
      'views/app/search-account/search-account.template.html',
    controller: function ($scope, $cookies, $http, $window, $location, $rootScope) {

      $scope.accountList = []
      // if ($cookies.get('sessionID') == undefined) {
      //   $window.location = '/login'
      // }
      name = ''

      $http({
        method: 'get',
        url: '/admin/getSearchAccount',
      }).success(function (datax) {
        if (datax != undefined) {
          // for (var i  in datax.data){
          $scope.accountList = datax.data
          // }
        }
      })





      $scope.addType = function () {
        $scope.accountList.push({ 'name': '', 'uid': '' })
      }

      $scope.remove = function (index) {
        // $scope.activityTypeList.splice(index,1)
        $http({
          method: 'post',
          url: '/admin/removeSearchAccount',
          data: { id: $scope.accountList[index]._id, session: $cookies.get('sessionID') }

        }).success(function (datax) {
          if (datax.code == 200) {
            $scope.accountList.splice(index, 1)
            swal('删除成功', '', "success").then(function () {
            })
          }
          if (datax.code == 401) {
            $window.location = '/login'
          }

        })
      }

      $scope.newSearchAccount = function () {
        var data = {}
        data.session = $cookies.get('sessionID')

        data.methods = JSON.stringify($scope.accountList)
        $http({
          method: 'post',
          url: '/admin/saveSearchAccount',
          data: data

        }).success(function (datax) {
          if (datax.code == 200) {
            swal('保存成功', '', "success").then(function () {
                    $http({
                method: 'get',
                url: '/admin/getSearchAccount',
              }).success(function (datax) {
                if (datax != undefined) {
                  // for (var i  in datax.data){
                  $scope.accountList = datax.data
                  // }
                }
              })
            })
          }
          if (datax.code == 401) {
            $window.location = '/login'
          }

        })
      }
      $scope.login = function(index){
        let data = {}
        data.name = $scope.accountList[index].name
        data.uid = $scope.accountList[index].uid
        $http({
          method: 'post',
          url: '/admin/searchlogin',
          data: data

        }).success(function (datax) {
          if (datax.code == 200) {
            swal('请在打开的浏览器中登录微博账号', '', "success").then(function () {
            })
          }
          if (datax.code == 401) {
            $window.location = '/login'
          }

        })
      }
    }
  })



